<?php
/**
 * @user: leo
 * @date: 2021/5/18
 * @createTime: 11:08
 * @company: 迈酷网络科技 http://www.maicooit.com
 */
namespace app\admin\controller\user;

use app\controller\Response;
use app\admin\logic\user\Menu as MenuLogic;
use app\BaseController;

class Menu extends BaseController{
    /**
     * @OA\Get(
     *   path="/admin/user/menuList",
     *   tags={"用户相关接口"},
     *   summary="菜单列表",
     *   @OA\Parameter(name="key", in="query", description="关键词", @OA\Schema(type="string")),
     *   @OA\Parameter(name="domain", in="query", description="租户", @OA\Schema(type="string")),
     *   @OA\Parameter(name="page", in="query", description="页码", @OA\Schema(type="string")),
     *   @OA\Parameter(name="pagesize", in="query", description="每页数量", @OA\Schema(type="string")),
     *   @OA\Response(
     *     response=200,
     *     description="A list with products"
     *   )
     * )
     */
    public function menuList($key="",$domain,$page=1,$pagesize=100){
        try{
            $menuAll = MenuLogic::MenuListLogic($key,$domain,$page,$pagesize);
            $menuAllarr = $menuAll->toArray();
            $menuTree = list_to_tree($menuAllarr['data'],'menu_id','pid','children');
            $list = getMenuWithId($menuTree);
            if($list){
                $res = Response::succJson($list,0,'获取数据成功！');
                return json($res);
            }else{
                $res = Response::errJson('',200,'获取数据失败！');
                return json($res);
            }
        }catch (\Exception $e){
            $res = Response::errJson('',$e->getCode(),$e->getMessage().'-'.$e->getLine());
            return json($res);
        }
    }
    /**
     * @OA\Post(
     *   path="/admin/user/menuAdd",
     *   tags={"用户相关接口"},
     *   summary=" 菜单新增",
     *   @OA\RequestBody(
     *     @OA\MediaType(
     *       mediaType="content-type/json",
     *         @OA\Schema(
     *           @OA\Property(description="租户", property="domain", type="string"),
     *           @OA\Property(description="父id", property="pid", type="string"),
     *           @OA\Property(description="菜单名称", property="menu_name", type="string"),
     *           @OA\Property(description="模块", property="model", type="string"),
     *           @OA\Property(description="前端组件名称", property="compname", type="string"),
     *           @OA\Property(description="前端组件路径", property="comppath", type="string"),
     *           required={"user_name"})
     *       )
     *     ),
     *   @OA\Response(
     *     response=200,
     *     description="A list with products"
     *   )
     * )
     */
    public function menuAdd(){
        try{
            $Info = request()->post();
            $menu = ['domain'=>$Info['domain'],'pid'=>$Info['pid'],'menu_name'=>$Info['menu_name'],'model'=>$Info['model'],'compname'=>$Info['compname'],'comppath'=>$Info['comppath'],'menu_icon'=>$Info['menu_icon']];
            $re = MenuLogic::MenuAddLogic($menu);
            if($re){
                $res = Response::succJson($re,0,'菜单添加成功！');
                return json($res);
            }else{
                $res = Response::errJson('',200,'菜单添加失败！');
                return json($res);
            }
        }catch (\Exception $e){
            $res = Response::errJson('',$e->getCode(),$e->getMessage());
            return json($res);
        }
    }
    /**
     * @OA\Post(
     *   path="/admin/user/menuSave",
     *   tags={"用户相关接口"},
     *   summary=" 菜单新增",
     *   @OA\RequestBody(
     *     @OA\MediaType(
     *       mediaType="content-type/json",
     *         @OA\Schema(
     *           @OA\Property(description="租户", property="domain", type="string"),
     *           @OA\Property(description="菜单id", property="menu_id", type="string"),
     *           @OA\Property(description="父id", property="pid", type="string"),
     *           @OA\Property(description="菜单名称", property="menu_name", type="string"),
     *           @OA\Property(description="模块", property="model", type="string"),
     *           @OA\Property(description="前端组件名称", property="compname", type="string"),
     *           @OA\Property(description="前端组件路径", property="comppath", type="string"),
     *           required={"user_name"})
     *       )
     *     ),
     *   @OA\Response(
     *     response=200,
     *     description="A list with products"
     *   )
     * )
     */
    public function menuSave(){
        try{
            $Info = request()->post();
            $menu_id = $Info['menu_id'];
            $menu = ['domain'=>$Info['domain'],'pid'=>$Info['pid'],'menu_name'=>$Info['menu_name'],'model'=>$Info['model'],'compname'=>$Info['compname'],'comppath'=>$Info['comppath'],'menu_icon'=>$Info['menu_icon']];
            $re = MenuLogic::MenuSaveLogic($menu_id,$menu);
            if($re!==false){
                $res = Response::succJson($re,0,'菜单编辑成功！');
                return json($res);
            }else{
                $res = Response::errJson('',200,'菜单编辑失败！');
                return json($res);
            }
        }catch (\Exception $e){
            $res = Response::errJson('',$e->getCode(),$e->getMessage());
            return json($res);
        }
    }
    /**
     * @OA\Get(
     *   path="/admin/user/menuDel",
     *   tags={"用户相关接口"},
     *   summary="菜单删除",
     *   @OA\Parameter(name="id", in="query", description="菜单id", @OA\Schema(type="string")),
     *   @OA\Response(
     *     response=200,
     *     description="A list with products"
     *   )
     * )
     */
    public function menuDel(){
        try{
            $Info = request()->get();
            $menu_id = $Info['id'];
            $re = MenuLogic::MenuDelLogic($menu_id);
            if($re){
                $res = Response::succJson($re,0,'菜单删除成功！');
                return json($res);
            }else{
                $res = Response::errJson('',200,'菜单删除失败！');
                return json($res);
            }
        }catch (\Exception $e){
            $res = Response::errJson('',$e->getCode(),$e->getMessage());
            return json($res);
        }
    }
}